package com.zkingsoft.laqeelin.action;

import static com.zkingsoft.common.constance.AppConstance.SAFEPATH;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.WebUtil;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.laqeelin.bean.BizHelpActivity;
import com.zkingsoft.laqeelin.service.BizHelpActivityService;

/**
 * @description (助力活动表)
 * @author 何明
 * @date 2018-09-01 10:35
 */
@Controller
@RequestMapping(value = "admin/bizHelpActivity")
public class BizHelpActivityAction extends BaseAction{

	@Autowired
	private BizHelpActivityService bizHelpActivityService;
	
	//记录编辑前的值Before_Edit_Value
	public static final String BEV="BizHelpActivity_BEV";
	
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(BizHelpActivity bizHelpActivity, PaginationVO pageVo) {
		return showList(bizHelpActivityService, bizHelpActivity, pageVo);
	}
   	
	/**
	 * 新增
	 */   
   	@RequestMapping(value =  "/addBizHelpActivity")
	public @ResponseBody AjaxResult addBizHelpActivity(BizHelpActivity bizHelpActivity) {
	 	int i=bizHelpActivityService.add(bizHelpActivity);
	 	if(i > 0){
	 		return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "(助力活动表)");
	 	}else {
			throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
		}
	}
	
	
	
	
	
	/**
	 * 修改
	 */   
   	@RequestMapping(value =  "/modifyBizHelpActivity")
	public @ResponseBody AjaxResult modifyBizHelpActivity(BizHelpActivity bizHelpActivity) {
	   		bizHelpActivity.setHacId(((BizHelpActivity)WebUtil.getSessionAttribute(BEV)).getHacId());
	   		AjaxResult result=modify(bizHelpActivityService, WebUtil.getSessionAttribute(BEV), bizHelpActivity, "(助力活动表)");
			WebUtil.removeSessionAttribute(BEV);
			return  result;
	}
	

	/**
	 * 修改活动
	 */   
	
   	@RequestMapping(value =  "/modifyActivity")
	public @ResponseBody AjaxResult modifyActivity( BizHelpActivity bizHelpActivity) {
   		int i=bizHelpActivityService.modifyByModel(bizHelpActivity);
	 	if(i > 0){
	 		return new AjaxResult(AjaxResult.STATUS_SUCCESS, "修改成功");
	 	}else {
	 		return new AjaxResult(AjaxResult.STATUS_FAIL, "修改失败");
		}
   	}
	
   	/**
	 * 进入修改界面
	 */   
   	@RequestMapping(value =  "/editForm")
	public String editForm(String id) {
		BizHelpActivity bizHelpActivity;
		if (id != null) {
			bizHelpActivity = bizHelpActivityService.findById(id);
			System.out.println(bizHelpActivity.getCodePosition());
			WebUtil.getRequest().setAttribute("obj", bizHelpActivity);
			WebUtil.setSessionAttribute(BEV, bizHelpActivity);
		}
		return "admin/biz/helpActivity-form";
	}
   	
   	
   	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
 		BizHelpActivity bizHelpActivity=new BizHelpActivity();
 		bizHelpActivity.setHacStatus(4);
 		bizHelpActivity.setActId(Long.valueOf(keys));
 		int i =bizHelpActivityService.modifyByModel(bizHelpActivity);
 		if(i==0){
 			return new AjaxResult(AjaxResult.STATUS_FAIL,"删除失败");
 		}
 		return new AjaxResult(AjaxResult.STATUS_SUCCESS,"删除成功");
	}
 	/**
	 * 上下架
	 */  
 	@RequestMapping(value = "/upOrdown")
	public @ResponseBody AjaxResult upOrdown (BizHelpActivity helpActivity) {
 		BizHelpActivity activity=bizHelpActivityService.findById(helpActivity.getActId().toString());
 		if(helpActivity.getHacStatus()==AppConstance.ACT_STATUS_FB && activity.getNum()>0){
 			return new AjaxResult(AjaxResult.STATUS_FAIL,"只能发布一个活动，请先下架其他活动");
 		}
 		BizHelpActivity bizHelpActivity=new BizHelpActivity();
 		bizHelpActivity.setHacStatus(helpActivity.getHacStatus());
 		bizHelpActivity.setActId(activity.getActId());
 		int i =bizHelpActivityService.modifyByModel(bizHelpActivity);
 		if(i==0){
 			return new AjaxResult(AjaxResult.STATUS_FAIL,"操作失败");
 		}
 		return new AjaxResult(AjaxResult.STATUS_SUCCESS,"操作成功");
	}
 	
	
   	/**
	 * 查询活动详情
	 */   
   	@RequestMapping(value =  "/findById")
	public @ResponseBody AjaxResult findById(String id) {
		BizHelpActivity bizHelpActivity = bizHelpActivityService.findById(id);
		return new AjaxResult(AjaxResult.STATUS_SUCCESS,Arrays.asList(bizHelpActivity));
	}
}